package com.dragon.modules.yuegou.client.ali.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayEcoMycarParkingConfigQueryModel;
import com.alipay.api.domain.AlipayEcoMycarParkingConfigSetModel;
import com.alipay.api.domain.AlipayEcoMycarParkingParkinglotinfoCreateModel;
import com.alipay.api.request.AlipayEcoMycarParkingConfigQueryRequest;
import com.alipay.api.request.AlipayEcoMycarParkingConfigSetRequest;
import com.alipay.api.request.AlipayEcoMycarParkingParkinglotinfoCreateRequest;
import com.alipay.api.response.AlipayEcoMycarParkingConfigQueryResponse;
import com.alipay.api.response.AlipayEcoMycarParkingConfigSetResponse;
import com.alipay.api.response.AlipayEcoMycarParkingParkinglotinfoCreateResponse;
import com.dragon.modules.yuegou.client.ali.request.ParkingConfigQueryRequest;
import com.dragon.modules.yuegou.client.ali.request.ParkingConfigSetRequest;
import com.dragon.modules.yuegou.client.ali.request.ParkinglotinfoCreateRequest;
import com.dragon.modules.yuegou.client.ali.service.ParkingService;
import com.dragon.modules.yuegou.properties.AlipayXcxConfig;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ParkingServiceImpl implements ParkingService {
    @Override
    public void configQuery(ParkingConfigQueryRequest request) {
        AlipayClient alipayClient = new DefaultAlipayClient(AlipayXcxConfig.ALI_URL,AlipayXcxConfig.APPID,
                AlipayXcxConfig.PRIVATE_KEY,"json","GBK",AlipayXcxConfig.ALIPAY_PUBLIC_KEY,"RSA2");
        AlipayEcoMycarParkingConfigQueryRequest queryRequest = new AlipayEcoMycarParkingConfigQueryRequest();
        AlipayEcoMycarParkingConfigQueryModel model = new AlipayEcoMycarParkingConfigQueryModel();
        model.setInterfaceName("alipay.eco.mycar.parking.isv.homepage");
        model.setInterfaceType("interface_page");
        queryRequest.setBizModel(model);
        try {
            AlipayEcoMycarParkingConfigQueryResponse response = alipayClient.execute(queryRequest);
            log.debug(JSON.toJSONString(response));
            if(response.isSuccess()){
                log.debug("调用成功");
            } else {
                log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void configSet(ParkingConfigSetRequest request) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",AlipayXcxConfig.APPID,
                AlipayXcxConfig.PRIVATE_KEY,"json","GBK",AlipayXcxConfig.ALIPAY_PUBLIC_KEY,"RSA2");
        AlipayEcoMycarParkingConfigSetRequest setRequest = new AlipayEcoMycarParkingConfigSetRequest();
        AlipayEcoMycarParkingConfigSetModel model = new AlipayEcoMycarParkingConfigSetModel();
        model.setAccountNo(request.getAccountNo());
        model.setMerchantLogo(request.getMerchantLogo());
        model.setMerchantName(request.getMerchantName());
        model.setMerchantServicePhone(request.getMerchantServicePhone());

        model.setInterfaceInfoList(request.getInterfaceInfoList());

        setRequest.setBizModel(model);

        try {
            AlipayEcoMycarParkingConfigSetResponse response = alipayClient.execute(setRequest);
            log.debug(JSON.toJSONString(response));
            if(response.isSuccess()){
                log.debug("调用成功");
            } else {
                log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void create(ParkinglotinfoCreateRequest request) {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayXcxConfig.APPID,
                AlipayXcxConfig.PRIVATE_KEY,"json","GBK",AlipayXcxConfig.ALIPAY_PUBLIC_KEY,"RSA2");
        AlipayEcoMycarParkingParkinglotinfoCreateRequest createRequest = new AlipayEcoMycarParkingParkinglotinfoCreateRequest();

        AlipayEcoMycarParkingParkinglotinfoCreateModel  createModel = new AlipayEcoMycarParkingParkinglotinfoCreateModel();
        createModel.setAgentId(AlipayXcxConfig.ISV_APPID);
        createModel.setMchntId(request.getMchnt_id());
        createModel.setOutParkingId(request.getOut_parking_id());
        createModel.setParkingAddress(request.getParking_address());
        createModel.setParkingFeeDescription(request.getParking_fee_description());
        createModel.setParkingLotType(request.getParking_lot_type());
        createModel.setShopingmallId(request.getShopingmall_id());
        createModel.setPayType(request.getPay_type());
        createModel.setParkingPoiid(request.getParking_poiid());
        createModel.setParkingName(request.getParking_name());
        createModel.setParkingMobile(request.getParking_mobile());
        createModel.setTimeOut(request.getTime_out());

        createRequest.setBizModel(createModel);
        try {
            AlipayEcoMycarParkingParkinglotinfoCreateResponse response = alipayClient.execute(createRequest);
            log.debug(JSON.toJSONString(response));
            if(response.isSuccess()){
                log.debug("调用成功");
            } else {
                log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

    }
}
